package com.luck.easyfoodbatch.ui;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hyphenate.EMCallBack;
import com.luck.easyfoodbatch.App;
import com.luck.easyfoodbatch.DemoHelper;
import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.dialog.CustomDialog;
import com.luck.easyfoodbatch.entity.User;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.T;
import com.luck.easyfoodbatch.util.Tools;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SettingActivity extends BaseActivity {

    @BindView(R.id.rl_bind)
    RelativeLayout rlBind;
    @BindView(R.id.rl_update)
    RelativeLayout rlUpdate;
    @BindView(R.id.rl_year_money)
    RelativeLayout rlYearMoney;
    @BindView(R.id.rl_help)
    RelativeLayout rlHelp;
    @BindView(R.id.rl_clear)
    RelativeLayout rl_clear;
    @BindView(R.id.btn_logout)
    Button btnLogout;
    @BindView(R.id.rl_yjfk)
    RelativeLayout rl_yjfk;
    private String role = "0";
    private List<Integer> roles = new ArrayList<>();// 权限标识
    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("action.app.logout")) {
                finish();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting, "设置", MODE_BACK_NAVIGATION);
        ButterKnife.bind(this);
        registerReceiver(receiver, "action.app.logout");
        user = (User) App.getInstance().readObject(HttpUtil.USER_KEY);
        role = user.getRole();
        roles.clear();
        String[] split = role.split(",");
        for (String code : split) {
            if (!Tools.isNull(code)) {
                roles.add(Integer.parseInt(code));
            }
        }
    }

    @OnClick({R.id.rl_bind, R.id.rl_update, R.id.rl_yjfk, R.id.rl_year_money, R.id.rl_help,R.id.rl_clear, R.id.btn_logout})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_bind:
                startAct(UpdateBindPhoneActivity.class);
                break;
            case R.id.rl_update:
                if (roles.contains(1) || roles.contains(2) || roles.contains(3)) {
                    T.showAnimToast(mContext, "抱歉，您没有操作权限~");
                } else {
                    startAct(UpdatePasswordActivity.class);
                }
                break;
            case R.id.rl_year_money:
                if (roles.size() > 1) {
                    if (roles.contains(3)){
                        startAct(AnnualFeeActivity.class);
                    } else {
                        T.showAnimToast(mContext, "抱歉，您没有操作权限~");
                    }
                } else {
                    if (roles.contains(1) || roles.contains(2)) {
                        T.showAnimToast(mContext, "抱歉，您没有操作权限~");
                    } else {
                        startAct(AnnualFeeActivity.class);
                    }
                }
                break;
            case R.id.rl_help:
                startAct(HelpCenterActivity.class);
                break;
            case R.id.rl_clear:
                  startAct(ClearCacheActivity.class);
                break;
            case R.id.btn_logout:
                showLogoutDialog();
                break;
            case R.id.rl_yjfk:
                // 意见反馈
                startAct(FeedbackActivity.class);
                break;
        }
    }


    /**
     * 退出登录
     */
    private void showLogoutDialog() {
        final CustomDialog dialog = new CustomDialog(mContext, App.screenWidth * 3 / 4,
                App.screenHeight / 4, R.layout.wind_base_dialog_xml, R.style.Theme_dialog);
        Button btn_cancel = (Button) dialog.findViewById(R.id.btn_cancel);
        Button btn_commit = (Button) dialog.findViewById(R.id.btn_commit);
        TextView tv_content = (TextView) dialog.findViewById(R.id.tv_content);
        tv_content.setText("确定退出登录吗？");
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        btn_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DemoHelper.getInstance().logout(false, new EMCallBack() {

                    @Override
                    public void onSuccess() {
                        runOnUiThread(new Runnable() {
                            public void run() {
                                sendBroadcast("action.app.logout");
                                App.getInstance().saveObject(null, HttpUtil.USER_KEY);
                                dialog.dismiss();
                                finish();
                            }
                        });
                    }

                    @Override
                    public void onProgress(int progress, String status) {
                    }

                    @Override
                    public void onError(int code, String message) {
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                Toast.makeText(mContext, "unbind devicetokens failed", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                });
            }
        });
        dialog.show();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (receiver != null) {
            unregisterReceiver(receiver);
        }
    }
}
